﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.SingleWell.Business;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using System.Data;
using Microsoft.Practices.Unity;
using Wuqi.Webdiyer;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellTotal : PageBase
    {
        #region 注册属性
        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IWellCatalogService m_IWellCatalogService
        {
            get;
            set;
        }

        /// <summary>
        /// 当前所选择的编目
        /// </summary>
        private WellCatalog m_WellCatalog
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["wellType"]) || Request.QueryString["wellType"].Length < 5)
                {
                    return this.m_IWellCatalogService.GetBaseCatalog();
                }
                else
                {
                    return this.m_IWellCatalogService.GetWellCatalogByID(Request.QueryString["wellType"]);
                }
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            pagerdo.PageSizeChanging += new EventHandler(pagerTransfered_PageSizeChanging);
            try
            {
                if (this.m_WellCatalog == null || this.m_WellCatalog.WELLCATALOG.Rows.Count == 0)
                {
                    throw new Exception("获取编目失败");
                }

                string dataType = Request.QueryString["enable"];
                if (string.IsNullOrEmpty(dataType) || dataType.Equals("single"))
                {
                    AddSingleWellBatch.Visible = false;
                    btnAddSingelWell.Visible = true;
                }
                else
                {
                    AddSingleWellBatch.Visible = true;
                    btnAddSingelWell.Visible = false;
                }

                if (!IsPostBack)
                {
                    this.BindData();
                }
                this.SetEntryDataEnable();
                this.InitAddSingleWellBatchUrl();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void pagerTransfered_PageSizeChanging(object sender, EventArgs e)
        {
            pagerdo.CurrentPageIndex = 1;
            this.BindData();
        }

        #region 私有方法
        private void SetEntryDataEnable()
        {
            //如果iswellcode字段为1，则允许创建子编目。
            if (this.m_WellCatalog.WELLCATALOG[0].ISWELLCODE == 1)
            {
                this.btnAddSingelWell.Visible = false;
                this.AddSingleWellBatch.Visible = true;
            }
            else
            {
                this.btnAddSingelWell.Visible = true;
                this.AddSingleWellBatch.Visible = false;
            }
        }

        private void BindData()
        {
            int sum = 0;
            //SingleWellModel oModel = this.m_ISingleWellService.GetTotal((pagerdo.CurrentPageIndex - 1) * pagerdo.PageSize, 100000, out sum, string.Format("wellcatalogid ='{0}'", this.m_WellCatalog.WELLCATALOG[0].WELLCATALOGID), string.Empty);
            //if (oModel != null && oModel.SINGLEWELL.Rows.Count > 0)
            //{
            //    this.gv_SingleWellTotal.DataSource = oModel.SINGLEWELL as DataTable;
            //    pagerdo.RecordCount = sum;
            //    this.gv_SingleWellTotal.DataBind();
            //}

            SingleWellModel singleWell = null;
            singleWell = m_ISingleWellService.GetTotal(0, 100000, out sum, string.Format("wellcatalogid ='{0}'", this.m_WellCatalog.WELLCATALOG[0].WELLCATALOGID), null);
            int recordCount = 0;
            this.gv_SingleWellTotal.DataSource = PageDataList.GetPagedTable(singleWell.Tables[0], this.pagerdo.CurrentPageIndex, this.pagerdo.PageSize, out recordCount);
            this.pagerdo.RecordCount = singleWell.Tables[0].Rows.Count;
            this.gv_SingleWellTotal.DataBind();
        }

        /// <summary>
        /// 初始化批量下载链接
        /// </summary>
        private void InitAddSingleWellBatchUrl()
        {
            string webAddress = "http://" + this.Request.Url.Authority + this.Request.ApplicationPath;
            webAddress = HttpUtility.UrlEncode(webAddress);
            if (!this.m_WellCatalog.WELLCATALOG[0].IsISWELLCODENull() && this.m_WellCatalog.WELLCATALOG[0].ISWELLCODE != 0)
            {
                this.AddSingleWellBatch.NavigateUrl = "~/FilesUploadClickOnce/FilesUpload.application?UserId=" + this.CurrentUser.User_ID + "&WebAddress=" + webAddress + "&CatalogId=" + this.m_WellCatalog.WELLCATALOG[0].WELLCATALOGID + "&Type=SingleWell";
            }
            else
            {
                this.AddSingleWellBatch.Enabled = false;
            }
        }
        #endregion

        #region 按钮事件
        protected void pagerTransfered_PageChanging(object sender, PageChangingEventArgs e)
        {
            pagerdo.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }

        /// <summary>
        /// 新建单井元数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddSingelWell_Click(object sender, EventArgs e)
        {
            Response.Redirect("SingleWellEntry.aspx?wellType=" + this.m_WellCatalog.WELLCATALOG[0].WELLCATALOGID);
        }
        #endregion
    }
}